package org.jeecg.modules.identifyRecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.identifyRecord.entity.IdentifyRecord;
import org.jeecg.modules.identifyRecord.service.IIdentifyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: ocr识别记录
 * @Author: jeecg-boot
 * @Date:   2025-03-20
 * @Version: V1.0
 */
@Api(tags="ocr识别记录")
@RestController
@RequestMapping("/identifyRecord/identifyRecord")
@Slf4j
public class IdentifyRecordController extends JeecgController<IdentifyRecord, IIdentifyRecordService> {
	@Autowired
	private IIdentifyRecordService identifyRecordService;
	
	/**
	 * 分页列表查询
	 *
	 * @param identifyRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ocr识别记录-分页列表查询")
	@ApiOperation(value="ocr识别记录-分页列表查询", notes="ocr识别记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(IdentifyRecord identifyRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<IdentifyRecord> queryWrapper = QueryGenerator.initQueryWrapper(identifyRecord, req.getParameterMap());
		Page<IdentifyRecord> page = new Page<IdentifyRecord>(pageNo, pageSize);
		IPage<IdentifyRecord> pageList = identifyRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param identifyRecord
	 * @return
	 */
	@AutoLog(value = "ocr识别记录-添加")
	@ApiOperation(value="ocr识别记录-添加", notes="ocr识别记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody IdentifyRecord identifyRecord) {
		identifyRecordService.save(identifyRecord);
		return Result.OK("添加成功！");
	}

	 /**
	  *   添加
	  *
	  * @param identifyRecord
	  * @return
	  */
	 @AutoLog(value = "ocr识别记录-批量添加")
	 @ApiOperation(value="ocr识别记录-批量添加", notes="ocr识别记录-批量添加")
	 @PostMapping(value = "/adds")
	 public Result<?> adds(@RequestBody List<IdentifyRecord> identifyRecord) {
		 identifyRecordService.saveBatch(identifyRecord);
		 return Result.OK("添加成功！");
	 }
	
	/**
	 *  编辑
	 *
	 * @param identifyRecord
	 * @return
	 */
	@AutoLog(value = "ocr识别记录-编辑")
	@ApiOperation(value="ocr识别记录-编辑", notes="ocr识别记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody IdentifyRecord identifyRecord) {
		identifyRecordService.updateById(identifyRecord);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ocr识别记录-通过id删除")
	@ApiOperation(value="ocr识别记录-通过id删除", notes="ocr识别记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		identifyRecordService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ocr识别记录-批量删除")
	@ApiOperation(value="ocr识别记录-批量删除", notes="ocr识别记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.identifyRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ocr识别记录-通过id查询")
	@ApiOperation(value="ocr识别记录-通过id查询", notes="ocr识别记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		IdentifyRecord identifyRecord = identifyRecordService.getById(id);
		if(identifyRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(identifyRecord);
	}

	@AutoLog(value = "ocr识别记录-通过罐车id查询")
	@ApiOperation(value="ocr识别记录-通过罐车id查询", notes="ocr识别记录-通过罐车id查询")
	@GetMapping(value = "/getByTankerId")
	public Result<?> getByTankerId(@RequestParam(name="tankerId",required=true) String tankerId) {
		LambdaQueryWrapper<IdentifyRecord> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(IdentifyRecord::getTankerId, tankerId);
		List<IdentifyRecord> identifyRecord = identifyRecordService.list(wrapper);
		return Result.OK(identifyRecord);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param identifyRecord
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, IdentifyRecord identifyRecord) {
        return super.exportXls(request, identifyRecord, IdentifyRecord.class, "ocr识别记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, IdentifyRecord.class);
    }

}
